package remoto;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import dominio.RemitoTransporte;
import dto.RemitoTransporteTO;

public class GestionRemitosTransporte {
	private static GestionRemitosTransporte instance;

	public static GestionRemitosTransporte getInstance() throws MalformedURLException, RemoteException, NotBoundException {
		if (instance == null)
			instance = new GestionRemitosTransporte();
		return instance;
	}

	private IGestionRemitosTransporte remoto;

	private GestionRemitosTransporte() throws MalformedURLException, RemoteException, NotBoundException {
		remoto = (IGestionRemitosTransporte) Naming.lookup("GestionRemitosTransporte");
	}

	public int guardarRemitoTransporte(RemitoTransporte r) throws RemoteException {
		return remoto.guardarRemitoTransporte(r.getDTO());
	}

	public RemitoTransporte buscarRemitoTransporte(int id) throws RemoteException {
		RemitoTransporteTO dto = remoto.buscarRemitoTransporte(id);
		if (dto != null)
			return RemitoTransporte.fromDTO(dto);
		return null;
	}

	public List<RemitoTransporte> listarRemitosPendientes() throws RemoteException {
		List<RemitoTransporte> lista = new ArrayList<RemitoTransporte>();
		List<RemitoTransporteTO> dtos = remoto.listarRemitosPendientes();
		for (RemitoTransporteTO dto : dtos)
			lista.add(RemitoTransporte.fromDTO(dto));
		return lista;
	}
}